

package com.tz.tzgw.pc.web.dao;

import com.tz.tzgw.pc.web.model.Banner;
import com.tz.tzgw.pc.web.model.Webcolumn;
import com.tz.tzgw.pc.web.model.ZtArticle;
import com.tz.tzgw.pc.web.model.ZtSet;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class WebcolumnDao {
    private final JdbcTemplate jdbcTemplate;

    public WebcolumnDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public List<Webcolumn> getList(Integer contentable) {
        String sql = "SELECT c1.id, c1.pid, c1.name, c1.sort_order, c1.contentable, c1.url, c1.module, c1.status, c2.id AS parent_id, c2.name AS parent_name FROM tz_webcolumn c1 LEFT JOIN tz_webcolumn c2 ON c1.pid = c2.id WHERE c1.status = 1";

        if (contentable > 0) {
            sql += " AND c1.contentable = " + contentable;
        }
        sql += " ORDER BY sort_order DESC ";
        List<Webcolumn> webcolumns = jdbcTemplate.query(sql, new RowMapper<Webcolumn>() {
            @Override
            public Webcolumn mapRow(ResultSet rs, int rowNum) throws SQLException {
                Long id = rs.getLong("id");
                Long pid = rs.getLong("pid");
                String name = rs.getString("name");
                Integer sort_order = rs.getInt("sort_order");
                Integer contentable = rs.getInt("contentable");
                String url = rs.getString("url");
                String module = rs.getString("module");
                Integer status = rs.getInt("status");
                Long parentId = rs.getLong("parent_id");
                String parentName = rs.getString("parent_name");

                Webcolumn webcolumn = new Webcolumn(id, pid, name, sort_order, status,contentable,url,module);
                if (parentId != 0) {
                    Webcolumn parent = new Webcolumn(parentId, 0L, parentName, 0, 0,0,"","");
                    webcolumn.setParent(parent);
                }

                return webcolumn;
            }
        });

        return webcolumns;


    }

    public int insertOrUpdateColumn(Webcolumn webcolumn) {
        if (webcolumn.getId() != null) {
            String sql = "UPDATE tz_webcolumn SET name=?, sort_order=?, status=?, contentable=?,url=?,module=? WHERE id=?";
            jdbcTemplate.update(sql, webcolumn.getName(), webcolumn.getSort_order(), webcolumn.getStatus(), webcolumn.getContentable(), webcolumn.getUrl(),webcolumn.getModule(),webcolumn.getId());
        } else {
            String sql = "INSERT INTO tz_webcolumn (pid, name, sort_order, status,contentable,url,module) VALUES (?, ?, ?, ?, ?, ?, ?)";
            jdbcTemplate.update(sql, webcolumn.getPid(), webcolumn.getName(), webcolumn.getSort_order(), webcolumn.getStatus(), webcolumn.getContentable(), webcolumn.getUrl(),webcolumn.getModule());
        }
        return  1;
    }

    public List<Webcolumn> getAllParentColumn(Long pid) {
        String sql = "SELECT * FROM tz_webcolumn where status=1 AND pid=? ORDER BY sort_order DESC ";
        return jdbcTemplate.query(sql,new Object[]{pid}, new BeanPropertyRowMapper<>(Webcolumn.class));
    }

    public Webcolumn getColumnByUrl(String url) {
        String sql = "SELECT * FROM tz_webcolumn WHERE url = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{url}, new BeanPropertyRowMapper<>(Webcolumn.class));
    }

    public Webcolumn convertZtSetToWebcolumn(ZtSet ztSet) {
        Webcolumn webcolumn = new Webcolumn();
        webcolumn.setId(ztSet.getId()); // 假设这两个类的 id 属性名称相同或可以通过 getter 方法获取
        webcolumn.setName(ztSet.getName()); // 同上，假设这两个类的 name 属性名称相同或可以通过 getter 方法获取
        webcolumn.setUrl(ztSet.getUrl());
        webcolumn.setSort_order(ztSet.getSort_order());
        // 根据需要，可以继续设置其他属性或执行其他转换逻辑
        return webcolumn;
    }

    public Integer deleteColumn(Integer id) {
        String sql = "DELETE FROM tz_webcolumn WHERE id = ?";
        jdbcTemplate.update(sql, id);
        return 1;
    }
}

